package com.bdqn.redissondemo.controller;

import org.redisson.Redisson;
import org.redisson.api.RLock;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import sun.awt.SunHints;

import javax.annotation.Resource;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@RestController
public class IndexController {

    @Resource
    Redisson redisson;

    @Resource
    StringRedisTemplate stringRedisTemplate;

    @RequestMapping(value = "/index")
    public Object index() {
        synchronized (this) {
            Integer count = Integer.parseInt(stringRedisTemplate.opsForValue().get("count"));
            if (count > 0) {
                int realCount = count - 1;
                stringRedisTemplate.opsForValue().set("count", String.valueOf(realCount));
                System.out.println("扣减库存成功,剩余库存:" + realCount);
                return realCount;
            } else {
                System.out.println("发生异常");
                return "发生异常";
            }
        }
    }

    @RequestMapping(value = "/redis")
    public Object redis() {
        System.out.println("redis");
        try {
            Boolean bool = stringRedisTemplate.opsForValue().setIfAbsent("lock", "lock");
            //避免死锁
            //Boolean bool = stringRedisTemplate.opsForValue().setIfAbsent("lock", "lock", 10, TimeUnit.SECONDS);
            if (bool == false) {
                return "error";
            }
            synchronized (this) {
                Integer count = Integer.parseInt(stringRedisTemplate.opsForValue().get("count"));
                if (count > 0) {
                    int realCount = count - 1;
                    stringRedisTemplate.opsForValue().set("count", String.valueOf(realCount));
                    System.out.println("扣减库存成功,剩余库存:" + realCount);
                    return realCount;
                } else {
                    System.out.println("发生异常");
                    return "发生异常";
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            stringRedisTemplate.delete("lock");
        }
        return "";
    }


    @RequestMapping(value = "/redismylock")
    public Object redisMyLock() {
        String mylock = UUID.randomUUID().toString();
        try {
            //Boolean bool = stringRedisTemplate.opsForValue().setIfAbsent("lock", "lock");
            //避免死锁
            Boolean bool = stringRedisTemplate.opsForValue().setIfAbsent("lock", mylock, 10, TimeUnit.SECONDS);
            if (bool == false) {
                return "error";
            }
            //synchronized (this) {
            Integer count = Integer.parseInt(stringRedisTemplate.opsForValue().get("count"));
            if (count > 0) {
                int realCount = count - 1;
                stringRedisTemplate.opsForValue().set("count", String.valueOf(realCount));
                System.out.println("扣减库存成功,剩余库存:" + realCount);
                return realCount;
            } else {
                System.out.println("发生异常");
                return "发生异常";
            }
            //}
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (mylock.equals(stringRedisTemplate.opsForValue().get("lock"))) {
                stringRedisTemplate.delete("lock");
            }
        }
        return "";
    }

    @RequestMapping(value = "/redissonmylock")
    public Object redissonMyLock() {
        //拿到锁对象
        RLock redissonLock = redisson.getLock("lock");
        try {
            //利用redisson加锁
            redissonLock.lock(30, TimeUnit.SECONDS);
            Integer count = Integer.parseInt(stringRedisTemplate.opsForValue().get("count"));
            if (count > 0) {
                int realCount = count - 1;
                stringRedisTemplate.opsForValue().set("count", String.valueOf(realCount));
                System.out.println("扣减库存成功,剩余库存:" + realCount);
                return realCount;
            } else {
                System.out.println("发生异常");
                return "发生异常";
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //释放锁
            redissonLock.unlock();
        }
        return "";
    }

}
